﻿using Ldt.DataEngine.DataFlowParse;
using Ldt.DataEngine.DataFlowParse.Model;
using Ldt.DataEngine.LogHandle;
using LdtDataEngin.Moudle;
using LdtJudge.DataEngine.CallInterface;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.CommHelp;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.Data.DataParseStorage;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.CommModel;
using LdtJudge.DataEngine.Model.DataModel.DataParseStorage;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtJudge.DataEngine.Model.EvidenceStandardizationModel;
using LdtJudge.DataEngine.Model.OptimizationModel;
using LdtJudge.DataEngine.Model.UnitModel;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;
using static Ldt.DataEngine.LogHandle.LogHandle;

namespace Ldt.DataEngin.Assemble
{
    public class MainBase
    {
        #region 基础信息

        private LogShow _uishow;
        private LogShowFontStyle _uilogShowFontStyle;
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="logType">消息类型</param>
        /// <param name="text">日志内容</param>
        private void DoSetLogtype(string text, LogMessageType logType = LogMessageType.Warn)
        {
            LogHType log = LogHType.Info;
            foreach (LogHType typ in Enum.GetValues(typeof(LogHType)))
            {
                if (((int)logType) == ((int)typ))
                {
                    log = typ;
                    break;
                }
            }
            //写入记录日志
            LogHandle.GetInitLogHandle.LogWrite(log, text);
            if (_uishow != null)
            {
                this._uishow(text, logType);
            }


        }

        public int IsEvidence { get; set; }

        /// <summary>
        /// 流程类型
        /// </summary>
        public enum FlowType
        {
            /// <summary>
            /// 分析流程
            /// </summary>
            Ana,
            /// <summary>
            /// 优选流程
            /// </summary>
            Opt,
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="logText">日志内容</param>
        /// <param name="fontStyle">样式</param>
        /// <param name="logType">日志输出等级</param>
        private void DoSetLogStyle(string logText, FontStyles fontStyle, LogMessageType logType)
        {
            //DoSetLogtype(logType,logText);
            LogHType log = LogHType.Info;
            foreach (LogHType typ in Enum.GetValues(typeof(LogHType)))
            {
                if (((int)logType) == ((int)typ))
                {
                    log = typ;
                    break;
                }
            }
            //写入记录日志
            LogHandle.GetInitLogHandle.LogWrite(log, logText);
            if (_uilogShowFontStyle != null)
            {
                this._uilogShowFontStyle(logText, fontStyle);
            }
        }
        /// <summary>
        /// 日志显示
        /// </summary>
        public LogShow LogShow
        {
            get { return DoSetLogtype; }
            set { _uishow = value; }
        }
        public InteractiveShowClass InteractiveShowClass { get; set; }

        /// <summary>
        /// 显示带有样式的日志
        /// </summary>
        public LogShowFontStyle LogShowFontStyle
        {
            get { return DoSetLogStyle; }
            set { _uilogShowFontStyle = value; }
        }
        /// <summary>
        /// 总体进度显示
        /// </summary>
        public ProcessShow ProcessShow { get; set; }

        /// <summary>
        /// 模块进度显示
        /// </summary>
        public ModuleProcessShow ModuleProcessShow { get; set; }

        /// <summary>
        /// XML配置信息
        /// </summary>
        public string XmlMessage { get; set; }
        /// <summary>
        /// 进度信息
        /// </summary>
        public InteractiveShowClass InteractiveShow { get; private set; }

        /// <summary>
        /// 分析结束标记
        /// </summary>
        public FinishShow Finishow { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public IConfigRetrieval configRetrieval;
        #endregion

        #region 对外发布消息内容--日志声明
        /// <summary>
        /// 进度显示
        /// </summary>
        /// <param name="fileId">文件ID</param>
        /// <param name="nvalue">进度值</param>
        public void SetProgress(string fileId, int nvalue, MainInputParameterModel inputParameterModel)
        {
            ProcessShow?.Invoke(fileId, nvalue,inputParameterModel);
        }

        /// <summary>
        /// 模块执行进度
        /// </summary>
        /// <param name="moduleName">模块名称</param>
        /// <param name="nvalue">进度值</param>
        public void SetModuleProcess(string moduleName, int nvalue, MainInputParameterModel inputParameterModel)
        {
            ModuleProcessShow?.Invoke(moduleName, nvalue,inputParameterModel);
        }

        public void SetModularModel(string message, string type)
        {
            //ModularModel?.Invoke(message, type);
        }

        /// <summary>
        /// 加载相关委托内容
        /// </summary>
        private void LoadDelegate()
        {
            InteractiveShow.LogShow = LogShow;//日志显示
            InteractiveShow.Finishow = Finishow;//结束分析
            InteractiveShow.ProcessShow = ProcessShow;//进度显示
        }
        #endregion

        #region 优选操作
        /// <summary>
        /// 数据回填数据
        /// </summary>
        /// <param name="sourceXml">回填信息</param>
        /// <param name="casetype">案件类型</param>
        /// <param name="courtID">法院ID</param>
        /// <param name="sD_CaseOnlyID">唯一ID</param>
        /// <param name="appId">用户ID</param>
        /// <param name="docType">文书类型</param>
        /// <param name="anjjd">案件阶段</param>
        /// <param name="msgid">消息ID</param>
        /// <param name="evidencePathList">证据事实关键数据集合</param>
        /// <param name="cCIDs">案由信息</param>
        /// <returns></returns>
        public string FillBackOptimization(string sourceXml, string casetype, string courtID, string sD_CaseOnlyID, string appId, string docType, string anjjd, string msgid,string evidencePathList, string cCIDs = "",string district="",string sfid="")
        {

            MainInputParameterModel inputParameter = new MainInputParameterModel()
            {
                CaseType = casetype,
                DocID = docType,
                SourceContent = "",
                SD_CaseOnlyID = sD_CaseOnlyID,
                FilePath = "",
                CaseCause = "",
                CourtId = courtID,
                AppId = appId,
                CaseID = sD_CaseOnlyID,
                MsgID = msgid,
                laststage= anjjd,
                FL_ID = "",
                SF_ID =sfid,
                IsSave = true,
                District=district
            };
            string opxml = "";
            if (!string.IsNullOrEmpty(sourceXml))
            {
                string messages = HttpHelp.DeCodeBase64new(65001, sourceXml.Replace('"', ' ')).Replace("&lt;", "<").Replace("&gt;", ">");
                dynamic dys = JsonConvert.DeserializeObject<dynamic>(messages);
                string ff = dys.changedata.ToString();
                List<ChangeDataModel> changeDataModelList = JsonConvert.DeserializeObject<List<ChangeDataModel>>(ff);
               
                DAL_An_anjiancunchu anjiancunchudal = new DAL_An_anjiancunchu();
                List<DataModel> dataModelOPList = new List<DataModel>();

                var anjiancunchuList = anjiancunchudal.GetList(sD_CaseOnlyID, "3");
                if (anjiancunchuList.Count > 0)
                {
                    DataSoursModel dataSoursModel = GetDataSoursModel(ReadDataXML(anjiancunchuList[0].Wenjianid));
                    dataModelOPList = dataSoursModel.dataModelList;

                }
                #region 运行写入操作
                ConfigFlowLoad configFlowLoad = new ConfigFlowLoad();
                DataEngin.Assemble.MainComponentBase main = new DataEngin.Assemble.MainComponentBase(configFlowLoad);
                main.inputParameter = inputParameter;
                main.LogShow = new LogShow(LogShow);
                main.ProcessShow = new ProcessShow(ProcessShow);
                main.ModuleProcessShow = new ModuleProcessShow(ModuleProcessShow);
                main.dataContext = new DataContext(new DataLayerSysStores(), new DyData_Analysis(), new InteractiveShowClass());
                main.dataContext.DataLayerAns.SatgeAnalysis = anjjd;
                main.dataContext.DataLayerAns.CaseType = casetype;
                main.dataContext.DataLayerAns.SourceCode = docType;
                main.dataContext.DataLayerAns.FengXiCanShu = new LdtJudge.DataEngine.Model.DataModel.DataParseStorage.An_fengxicanshu();
                //执行第一个模块；
                main.dataContext.DataLayerAns.SF_ID = inputParameter.SF_ID;
                main.dataContext.DataLayerAns.SourceCode = inputParameter.DocID;
                #region  对上下文的基础变量做赋值，主要与输入参数inputParameter做对应
                main.dataContext.DataLayerAns.CaseType = inputParameter.CaseType;
                main.dataContext.DataLayerAns.CourtId = inputParameter.CourtId;
                main.dataContext.DataLayerAns.AppId = inputParameter.DocID;
                main.dataContext.DataLayerAns.MsgId = inputParameter.MsgID;
                main.dataContext.DataLayerAns.SF_ID = inputParameter.SF_ID;
                main.dataContext.DataLayerAns.FL_ID = inputParameter.FL_ID;
                main.dataContext.DataLayerAns.CaseId = inputParameter.CaseID;
                main.dataContext.DataLayerAns.SourceCode = inputParameter.DocID;
                main.dataContext.DataLayerAns.IsEvidence = IsEvidence;
                main.dataContext.DataLayerAns.EvidenceXpathList = evidencePathList;
                main.dataContext.DataCommon.CaseType = casetype;
                main.dataContext.DataCommon.SourceCode = docType;
                main.dataContext.DataCommon.MsgId = inputParameter.MsgID;

                if (string.IsNullOrEmpty(cCIDs))
                {
                    DAL_An_anjianxinxi anjianxin = new DAL_An_anjianxinxi();
                    An_anyouxinxi anyouxinxi = anjianxin.GetAnYouxinxiByAnjianid(inputParameter.CaseID);
                    if (anyouxinxi != null)
                        cCIDs = anyouxinxi.Anyoucode;
                }

                // 案由信息
                if (!string.IsNullOrEmpty(cCIDs))
                {
                    GetSysCaseCause dalCause = new GetSysCaseCause();
                    foreach (var ccid in cCIDs.Split(','))
                    {
                        CaseCauseModel MCause = new CaseCauseModel();
                        if (inputParameter.CaseType == "xzzf")
                        {
                            inputParameter.CaseCause = ccid;
                            MCause = dalCause.GetModelByCauseNamexzzf(ccid, inputParameter.CaseType);
                        }
                        else
                        {
                            MCause = dalCause.GetModelByCauseName(ccid, inputParameter.CaseType);
                        }

                        if (main.dataContext.DataLayerAns.CaseCausesList == null)
                            main.dataContext.DataLayerAns.CaseCausesList = new List<CaseCauseModel>();
                        main.dataContext.DataLayerAns.CaseCausesList.Add(MCause);
                        main.dataContext.DataLayerAns.ExtractCaseCausesList = new List<CaseCauseModel>();
                        main.dataContext.DataLayerAns.ExtractCaseCausesList.Add(MCause);
                    }
                }
                
                //案件信息
                main.dataContext.DataLayerAns.Anjianxinxi = new An_anjianxinxi()
                {
                    Anhao = "",
                    Anjianid = inputParameter.CaseID,
                    Anjianleixing = inputParameter.CaseType,
                    Fayuanid = "",
                    Gengxinshijian = DateTime.Now,
                    Zhuangtai = 1
                };
                main.dataContext.DataLayerAns.Shujuneirong = new An_shujuneirong()
                {
                    Anjianid = inputParameter.CaseID,
                    Fl_id = inputParameter.FL_ID,
                    Sf_id = inputParameter.SF_ID,
                    Shujuid = Snowflake.Instance().GetId().ToString(),
                    Shujuyuanbianhao = inputParameter.DocID,
                    Shujuyuandierbiaoshi = "",
                    Shujuyuanleixing = 1,
                    Shujuyuanzhuangtai = 1
                };
                #endregion
                main.dataContext.DataLayerAns.FengXiCanShu.Anjianleixing = inputParameter.CaseType;
                main.dataContext.DataLayerAns.FengXiCanShu.Fayuanid = inputParameter.CourtId;
                main.dataContext.DataLayerAns.changeDataModelList = new List<ChangeDataModel>();
                main.dataContext.DataLayerAns.changeDataModelList = changeDataModelList;
                main.dataContext.DataLayerAns.FengXiCanShu.Fl_id = inputParameter.FL_ID;
                main.dataContext.DataLayerAns.FengXiCanShu.Juanzongbianhao = inputParameter.DocID;
                main.dataContext.DataLayerAns.FengXiCanShu.Yingyongid = inputParameter.AppId;
                main.dataContext.DataLayerAns.FengXiCanShu.Canshuid = Snowflake.Instance().GetId().ToString();
                main.dataContext.DataLayerAns.FengXiCanShu.Juanzongneirong = inputParameter.SourceContent.Replace("&#x13;", "").Replace("&#x14;", "").Replace("&#x15;", "").Replace("&#xD;", "");
                main.GetFlowConfig(ConfigFlowLoad.FlowType.Opt);
                main.ExtractWrap();
                #endregion
                if (main.dataContext.DataLayerOpt != null && main.dataContext.DataLayerOpt.OptimizationXml != null && !string.IsNullOrEmpty(main.dataContext.DataLayerOpt.OptimizationXml.OuterXml))
                {
                    opxml = main.dataContext.DataLayerOpt.OptimizationXml.OuterXml;
                }
            }
            return opxml;
        }


        /// <summary>
        /// 新处理方式
        /// TODO:ZSW
        /// </summary>
        /// <param name="fillBackOptParameter">回填数据集合</param>
        public void FillBackOptimization(FillBackOptParameterModel fillBackOptParameter) 
        {
            ConfigFlowLoad configFlowLoad = new ConfigFlowLoad();
            //拉取流程配置请求参数
            RequestPara processRequ = new RequestPara();
            processRequ.CaseType = fillBackOptParameter.CaseType;
            processRequ.AppId = fillBackOptParameter.AppId;
            processRequ.CourtId = fillBackOptParameter.CourtID;

           
            if (fillBackOptParameter.Contents!=null && fillBackOptParameter.Contents.Any())
            {
                List<string> groupDoc = new List<string>();
                Dictionary<string, List<ConfigModel>> configDic = new Dictionary<string, List<ConfigModel>>();
                foreach (var item in fillBackOptParameter.Contents)
                {
                    string sourceCode = string.Empty;
                    if (item.IsEvidence == 1)
                        sourceCode = "evidencedata";
                    else
                        sourceCode = item.SourceCode;
                    processRequ.DocID = sourceCode;
                    if(!configDic.ContainsKey(sourceCode))
                    {
                        List<ConfigModel> configs = configFlowLoad.LoadFlowInfo(processRequ, ConfigFlowLoad.FlowType.Opt);
                        configDic.Add(sourceCode, configs);
                        if(configs.Any(x=>!string.IsNullOrWhiteSpace(x.GroupStepCode)))
                        {
                            groupDoc.Add(sourceCode);
                        }
                    }
                }

                if(groupDoc!=null && groupDoc.Any())
                {
                    foreach (var item in groupDoc)
                    {
                        List<DataContent> dataContents = new List<DataContent>();
                        if(item== "evidencedata")
                            dataContents = fillBackOptParameter.Contents.Where(x => x.IsEvidence == 1).ToList();
                        else
                            dataContents= fillBackOptParameter.Contents.Where(x => x.SourceCode == item).ToList();
                        if(dataContents!=null && dataContents.Any())
                        {
                            foreach (var content in dataContents)
                            {
                                fillBackOptParameter.Contents.Remove(content);
                            }
                            FillBackDataModel fillBackDataModel = new FillBackDataModel();
                            fillBackDataModel.ProcessShow = new ProcessShow(ProcessShow);
                            fillBackDataModel.ModuleProcessShow = new ModuleProcessShow(SetModuleProcess);
                            List<ConfigModel> configs = configDic[item];
                            fillBackDataModel.configRetrieval = configRetrieval;
                            
                            fillBackDataModel.LogShow = new LogShow(LogShow);//日志显示
                         
                            fillBackDataModel.FillBackData(dataContents, fillBackOptParameter.CaseType, fillBackOptParameter.CourtID, fillBackOptParameter.Outrelatid, fillBackOptParameter.AppId, item, fillBackOptParameter.laststage, fillBackOptParameter.MsgId, "", configs, fillBackOptParameter.CCIDs,fillBackOptParameter.OredContents);
                        }
                    }
                }
                foreach (var item in fillBackOptParameter.Contents)
                {
                    List<DataContent> dataContents = new List<DataContent>();
                    dataContents.Add(item);
                    FillBackDataModel fillBackDataModel = new FillBackDataModel();
                    fillBackDataModel.ProcessShow = new ProcessShow(ProcessShow);
                    fillBackDataModel.ModuleProcessShow = new ModuleProcessShow(SetModuleProcess);
                    fillBackDataModel.LogShow = new LogShow(LogShow);
                    List<ConfigModel> configs = configDic[item.SourceCode];
                    fillBackDataModel.configRetrieval = configRetrieval;
                    //if (fillBackDataModel.InteractiveShowClass.LogShow == null)
                    //    fillBackDataModel.InteractiveShowClass.LogShow = new LogShow(LogShow);//日志显示
                    fillBackDataModel.FillBackData(dataContents, fillBackOptParameter.CaseType, fillBackOptParameter.CourtID, fillBackOptParameter.Outrelatid, fillBackOptParameter.AppId, item.SourceCode, fillBackOptParameter.laststage, fillBackOptParameter.MsgId, "", configs, fillBackOptParameter.CCIDs, fillBackOptParameter.OredContents);
                }
            }
        }
        
        /// <summary>
        /// 获取XML数据
        /// </summary>
        /// <param name="id">XMLid</param>
        /// <returns></returns>
        public XmlDocument ReadDataXML(string id)
        {
            XmlDocument xmlDocument = new XmlDocument();
            dynamic message = HttpHelp.HttpGet(ConfigRetrieval.GetConfigRetrieval.GetAppSettingString("ldt.elementcenterfilestore") + "/api/XmlDataManage/GetXmlContent/" + id, "");
            string messagex = message;
            xmlDocument.LoadXml(messagex.Replace("&#x13;", "").Replace("&#x14;", "").Replace("&#x15;", "").Replace("&#xD;", ""));
            return xmlDocument;
        }
        /// <summary>
        /// 转换出数据源实体
        /// </summary>
        /// <returns></returns>
        public DataSoursModel GetDataSoursModel(XmlDocument xmlDocument)
        {
            DataSoursModel soursModel = new DataSoursModel();
            if (!string.IsNullOrEmpty(xmlDocument.OuterXml))
            {
                soursModel = XmlUtility.DeserializeToObject<DataSoursModel>(xmlDocument.OuterXml.Replace("&#x", ""));
                foreach (DataModel dataModel in soursModel.dataModelList)
                {
                    dataModel.XmlPath = dataModel.XmlPath.Replace("\\", "/");
                }
            }

            return soursModel;
        }

        #endregion

        /// <summary>
        /// 证据文书回填信息
        /// </summary>
        /// <param name="caseEvidenceInfo">数据回填信息</param>
        public void EvidenceBackfill(EvidenceFillBackModel caseEvidenceInfo)
        {
            //try
            //{
            //    #region 证据文书回填
            //    Stopwatch swtext = new Stopwatch();
            //    SetModuleProcess("证据回填模块:证据回填优选模块", 0);
            //    swtext.Start();
            //    OptimizationNew optimizationNew = new OptimizationNew();
            //    dynamic optimizatConifg = JsonConvert.DeserializeObject<dynamic>(XmlMessage);//
            //    string zstpz = "";
            //    optimizationNew.dataLayerStores = dynamicDataStores.dataLayerStores;
            //    optimizationNew.InteractiveShow = dynamicDataStores.InteractiveShow;
            //    //拉取配置文件成功
            //    string moduleConfig = optimizatConifg.moduleConfig.ToString();
            //    List<ConfigurationFileModel> configurationFileList = JsonConvert.DeserializeObject<List<ConfigurationFileModel>>(moduleConfig);
            //    foreach (ConfigurationFileModel fileModel in configurationFileList)
            //    {
            //        string code = fileModel.Code;
            //        switch (code.ToLower())
            //        {
            //            case "caseelementcfg":
            //                optimizationNew.Casexmlcontst = fileModel.ConfigContent;
            //                break;
            //            case "elementpath":
            //                string cfgStr = fileModel.ConfigContent;
            //                XDocument xdoc = XDocument.Parse(cfgStr);
            //                XElement root = xdoc.Root;
            //                foreach (XElement booktype in root.Elements("booktype"))
            //                {
            //                    LocationModel locationModel = new LocationModel();
            //                    string bookName = string.Empty;
            //                    if (booktype.Attribute("code") != null)
            //                    {
            //                        locationModel.DocType = booktype.Attribute("code").Value;
            //                    }
            //                    if (booktype.Attribute("path") != null)
            //                    {
            //                        locationModel.DocPath = booktype.Attribute("path").Value;
            //                    }
            //                    foreach (XElement element in booktype.Elements("element"))
            //                    {
            //                        if (element.Attribute("path") != null)
            //                        {
            //                            locationModel.EelementPaht = element.Attribute("path").Value;
            //                        }

            //                    }
            //                    foreach (XElement element in booktype.Elements("mergelement"))
            //                    {
            //                        if (element.Attribute("path") != null)
            //                        {
            //                            locationModel.MergeElmentPath = element.Attribute("path").Value;
            //                        }
            //                    }
            //                    foreach (XElement element in booktype.Elements("reasonelement"))
            //                    {
            //                        string caseCauseRoute = string.Empty;
            //                        if (element.Attribute("path") != null)
            //                        {
            //                            locationModel.ReasoneELementPath = element.Attribute("path").Value;
            //                        }
            //                    }
            //                    dynamicDataStores.dataLayerStores.locationModelList.Add(locationModel);
            //                }
            //                optimizationNew.dataLayerStores = dynamicDataStores.dataLayerStores;
            //                optimizationNew.InteractiveShow = dynamicDataStores.InteractiveShow;

            //                break;
            //            case "casecauseelementcfg":
            //                optimizationNew.Elementxmlcontst = fileModel.ConfigContent;
            //                break;
            //            case "converttoentity":
            //                zstpz = fileModel.ConfigContent;
            //                break;
            //            default:
            //                break;
            //        }
            //    }
            //    dynamicDataStores.InteractiveShow.LogShow("-----------------进入证据回填优选----------------");
            //    LdtJudge.DataEngine.StoreModel.EvidenceStandardization.EvidenceFillBack evidenceFillBack = new LdtJudge.DataEngine.StoreModel.EvidenceStandardization.EvidenceFillBack(caseEvidenceInfo.CaseInfo.OutRealtId)
            //    {
            //        dataLayerStores = dynamicDataStores.dataLayerStores,
            //        Casexmlcontst = optimizationNew.Casexmlcontst,
            //        Elementxmlcontst = optimizationNew.Elementxmlcontst,
            //        InteractiveShow = dynamicDataStores.InteractiveShow
            //    };

            //    evidenceFillBack.EvidenceFillBackTo(caseEvidenceInfo);
            //    if (string.IsNullOrEmpty(zstpz))
            //    {
            //        dynamicDataStores.InteractiveShow.LogShow("转实体配置为空");
            //    }
            //    else
            //    {
            //        DataConverTable dataConverTable = new DataConverTable(zstpz);
            //        dynamicDataStores.InteractiveShow.LogShow("转换XML：" + dynamicDataStores.dataLayerStores.OptimizationXml.OuterXml);
            //        string datamessage = dataConverTable.SaveDictabel(dynamicDataStores.dataLayerStores.OptimizationXml.SelectSingleNode("/*").ParentNode, dynamicDataStores);
            //        try
            //        {
            //            string data = HttpHelp.HttpPostJson(configRetrieval.GetAppSettingString("wordparseapi") + "/AnalysisData/v1/AnalysisDataContentThree/66666", datamessage);
            //            dynamicDataStores.InteractiveShow.LogShow("转换成功：" + data);
            //        }
            //        catch (Exception e)
            //        {
            //            dynamicDataStores.InteractiveShow.LogShow("转换失败" + e.ToString());
            //        }
            //    }
            //    //DataResultSave dataResultSave = new DataResultSave() { dynamicDataStores = dynamicDataStores };
            //    //dataResultSave.OptimizationSave();
            //    DeductionDataAssemble deductionDataAssemble = new DeductionDataAssemble() { dynamicDataStores = dynamicDataStores };
            //    deductionDataAssemble.OptimizationDeductionSave(configRetrieval.GetConnectionString("systemapi"));

            //    dynamicDataStores.InteractiveShow.LogShow("-----------------进入证据回填优选----------------");
            //    swtext.Stop();
            //    SetModuleProcess("优选模块:优选模块_" + swtext.Elapsed.TotalMilliseconds.ToString("0"), 100);
            //    //ModularModel(JsonConvert.SerializeObject(dynamicDataStores.dataLayerStores), "0");
            //    #endregion
            //}
            //catch (Exception ex)
            //{
            //    //dynamicDataStores.InteractiveShow.LogShow("优选发生错误:" + ex.Message, LogMessageType.Error);
            //    throw;
            //}

        }
        
        /// <summary>
        /// 指定文书删除
        /// </summary>
        /// <param name="caseType">案件类型</param>
        /// <param name="caseId">案件ID</param>
        /// <param name="dataSource">文书编号</param>
        /// <param name="msgState">文书删除状态</param>
        /// <param name="errMsg">错误消息</param>
        public void InstrumentDeletion(string caseType, string caseId, string dataSource,out string msgState,out string errMsg)
        {
            msgState = string.Empty;
            errMsg = string.Empty;
            if (string.IsNullOrWhiteSpace(caseId))
                throw new ArgumentNullException("CaseInfo-CaseId", "传入案件ID为空");
            if (string.IsNullOrWhiteSpace(caseType))
            {
                DAL_An_anjianxinxi anjianxinxiDal = new DAL_An_anjianxinxi();
                var anjianxinx = anjianxinxiDal.GetAnjianxinxiById(caseId);
                if (anjianxinx != null)
                    caseType = anjianxinx.Anjianleixing;
            }
            if (string.IsNullOrWhiteSpace(caseType))
                throw new ArgumentNullException("CaseInfo-CaseType", "传入案件类型编号为空");
            if (string.IsNullOrWhiteSpace(dataSource))
                throw new ArgumentNullException("DocInfo-SourceCode", "传入数据源编号为空");
            DataContext dataContext = new DataContext(new DataLayerSysStores(), null, new DyData_Optimize(), new InteractiveShowClass());
            if (dataContext.InteractiveShow.LogShow == null)
                dataContext.InteractiveShow.LogShow = new LogShow(LogShow);
            dataContext.DataLayerOpt.CaseType = caseType.Trim();
            dataContext.DataLayerOpt.CaseId = caseId.Trim();
            dataContext.DataLayerOpt.SourceCode = dataSource.Trim();
            try
            {
                Stopwatch swtext = new Stopwatch();
                swtext.Start();
                dataContext.InteractiveShow.LogShow("-----------------进入数据源删除----------------");
                DataSourceManage dataSourceManage = new DataSourceManage() { dataContext = dataContext };
                dataSourceManage.DataSourceRemove();
                swtext.Stop();
                dataContext.InteractiveShow.LogShow("-----------------数据源删除完成----------------用时:"+ swtext.Elapsed.TotalMilliseconds.ToString("0"));
                msgState = "1";
            }
            catch (Exception ex)
            {
                msgState = "0";
                errMsg = ex.ToString();
                dataContext.InteractiveShow.LogShow("数据源删除错误:" + ex.ToString(), LogMessageType.Error);
            }
        }
    }
}
